package com.xingdata.jjxc.utils;

import java.util.ArrayList;

/**
 * 
 * 
 * 项目名称：JJXC_VPN 类名称：OBDTools 类描述： OBD工具类 创建人：JiangMin 创建时间：2015-7-10 下午3:10:26
 * 修改备注：
 * 
 * @version
 * 
 */
public class OBDTools {
	private static int NORMAL = 1;
	private static int WARNING = 2;
	private static int ERROR = 3;
	private static int UNKNOWN = 0;
	private static int inMin = 0;
	private static int inMax = 0;

	/**
	 * 获取发动机转速
	 * 
	 * @param list
	 * @return
	 */
	public static String getEngineSpeed(ArrayList<String> list) {
		int inODB = 0;
		int inResult = 0;
		inMin = 600;
		inMax = 6000;
		if (list.size() == 15) {
			inODB = Integer.parseInt(list.get(2));
			if (inMin < inODB && inODB < inMax) {
				inResult = NORMAL;
			} else if (inMin == inODB || inODB == inMax) {
				inResult = WARNING;
			} else {
				inResult = ERROR;
			}
		} else {
			inResult = UNKNOWN;
		}

		return getODBValue(inResult);
	}

	/**
	 * 获取发送机负荷
	 * 
	 * @param list
	 * @return
	 */
	public static String getEngineCharge(ArrayList<String> list) {
		double dODB = 0.0;
		int inResult = 0;
		inMin = 0;
		inMax = 100;
		if (list.size() == 15) {
			dODB = Double.parseDouble(list.get(5));
			if (inMin <= dODB && dODB <= 90) {
				inResult = NORMAL;
			} else if (90 < dODB && dODB < inMax) {
				inResult = WARNING;
			} else {
				inResult = ERROR;
			}
		} else {
			inResult = UNKNOWN;
		}

		return getODBValue(inResult);
	}

	/**
	 * 冷却液温度
	 * 
	 * @param list
	 * @return
	 */
	public static String getCoolantTemperaturel(ArrayList<String> list) {
		int inODB = 0;
		int inResult = 0;
		inMin = -25;
		inMax = 120;
		if (list.size() == 15) {
			inODB = Integer.parseInt(list.get(6));
			if (inMin <= inODB && inODB <= 110) {
				inResult = NORMAL;
			} else if (110 < inODB && inODB <= inMax) {
				inResult = WARNING;
			} else {
				inResult = ERROR;
			}
		} else {
			inResult = UNKNOWN;
		}

		return getODBValue(inResult);
	}

	/**
	 * 电瓶电压
	 * 
	 * @param list
	 * @return
	 */
	public static String getBatteryVolts(ArrayList<String> list) {
		Double dODB = 0.0;
		int inResult = 0;
		inMin = 11;
		inMax = 16;
		if (list.size() == 15) {
			dODB = Double.parseDouble((list.get(1)));
			if (inMin < dODB && dODB < inMax) {
				inResult = NORMAL;
			} else if (inMin == dODB || dODB == inMax) {
				inResult = WARNING;
			} else {
				inResult = ERROR;
			}
		} else {
			inResult = UNKNOWN;
		}
		return getODBValue(inResult);
	}

	private static String getODBValue(int inResult) {
		String strResult = "";
		switch (inResult) {
		case 0:
			strResult = "未知";
			break;
		case 1:
			strResult = "正常";
			break;
		case 2:
			strResult = "提醒";
			break;
		case 3:
			strResult = "故障";
			break;

		default:
			break;
		}
		return strResult;

	}

	/**
	 * 节气门开度
	 * 
	 * @param list
	 * @return
	 */
	public static String getThrottleValve(ArrayList<String> list) {
		int inODB = 0;
		int inResult = 0;
		inMin = 0;
		inMax = 100;
		if (list.size() == 15) {
			inODB = Integer.parseInt(list.get(4));
			if (inMin <= inODB && inODB < inMax) {
				inResult = NORMAL;
			} else if (inODB == inMax) {
				inResult = WARNING;
			} else {
				inResult = ERROR;
			}
		} else {
			inResult = UNKNOWN;
		}

		return getODBValue(inResult);
	}

	public static ArrayList<String> StringToArrayList(String odb) {
		ArrayList<String> arrayList = new ArrayList<String>();
		ArrayList<String> indexList = new ArrayList<String>();
		for (int i = 0; i < odb.length(); i++) {
			int index = odb.substring(i, i + 1).indexOf(",");
			if (index > -1) {
				if (indexList.size() == 0) {
					arrayList.add(odb.substring(0, i));
				} else {
					int in = Integer
							.parseInt(indexList.get(indexList.size() - 1));
					arrayList.add(odb.substring(in + 1, i));
				}
				indexList.add(i + "");
			}
		}
		// System.out.println("------------------------");
		// for (int i = 0; i < arrayList.size(); i++) {
		// System.out.println(arrayList.get(i));
		// }
		return arrayList;

	}

}
